package com.studyroom.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.studyroom.common.ApiResponse;
import com.studyroom.common.Result;
import com.studyroom.entity.Feedback;
import com.studyroom.service.FeedbackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/admin/feedbacks")
@PreAuthorize("hasRole('ADMIN')")
public class AdminFeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    @GetMapping
    public Result<Page<Feedback>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String content,
            @RequestParam(required = false) Integer status) {
        
        try {
            Page<Feedback> page = feedbackService.getAdminFeedbackList(current, size, content, status);
            return Result.success(page);
        } catch (Exception e) {
            log.error("获取反馈列表失败", e);
            return Result.error("获取反馈列表失败");
        }
    }

    @GetMapping("/{id}")
    public Result<Feedback> getById(@PathVariable Long id) {
        try {
            Feedback feedback = feedbackService.getById(id);
            return Result.success(feedback);
        } catch (Exception e) {
            log.error("获取反馈详情失败", e);
            return Result.error("获取反馈详情失败");
        }
    }

    @PutMapping("/{id}/reply")
    public Result<?> reply(@PathVariable Long id, @RequestBody Feedback feedback) {
        try {
            feedbackService.reply(id, feedback.getReply());
            return Result.success();
        } catch (Exception e) {
            log.error("回复反馈失败", e);
            return Result.error("回复反馈失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result<?> deleteFeedback(@PathVariable Long id) {
        try {
            feedbackService.deleteFeedback(id);
            return Result.success("留言删除成功");
        } catch (Exception e) {
            return Result.error("删除失败: " + e.getMessage());
        }
    }
} 